package cn.schoolwow.workflow;

import cn.schoolwow.quickdao.QuickDAO;
import cn.schoolwow.quickdao.domain.DAO;
import cn.schoolwow.quickdao.domain.database.dml.RepeatInstanceStrategy;
import cn.schoolwow.quickflow.QuickFlow;
import cn.schoolwow.quickflow.QuickFlowBuilder;
import cn.schoolwow.workflow.domain.instance.WorkFlowInstanceListener;
import cn.schoolwow.workflow.domain.task.WorkFlowTaskListener;
import cn.schoolwow.workflow.module.common.domain.QuickWorkFlowConfig;
import cn.schoolwow.workflow.module.common.domain.QuickWorkFlowOption;
import cn.schoolwow.workflow.module.initial.flow.InitialDatabaseFlow;
import cn.schoolwow.workflow.module.parent.kit.WorkFlowInvocationHandler;
import cn.schoolwow.workflow.module.task.job.service.CheckTaskTimeoutServiceFlow;

import javax.sql.DataSource;
import java.lang.reflect.Proxy;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class QuickWorkFlowBuilder {
    private DAO dao;

    private QuickWorkFlowOption quickWorkFlowOption = new QuickWorkFlowOption();

    public static QuickWorkFlowBuilder newInstance() {
        return new QuickWorkFlowBuilder();
    }

    public QuickWorkFlowBuilder dataSource(DataSource dataSource){
        QuickDAO quickDAO = QuickDAO.newInstance()
                .dataSource(dataSource)
                .packageName("cn.schoolwow.workflow.entity","workflow")
                .repeatInstanceStrategy(RepeatInstanceStrategy.CheckAndThrow);
        QuickFlowBuilder.startFlow(new InitialDatabaseFlow())
                .putInstanceData(quickDAO, QuickDAO.class)
                .execute();
        DAO dao = quickDAO.build();
        this.dao = dao;
        return this;
    }

    public QuickWorkFlowBuilder workFlowInstanceListener(WorkFlowInstanceListener workFlowInstanceListener){
        quickWorkFlowOption.workFlowInstanceListener = workFlowInstanceListener;
        return this;
    }

    public QuickWorkFlowBuilder workFlowTaskListener(WorkFlowTaskListener workFlowTaskListener){
        quickWorkFlowOption.workFlowTaskListener = workFlowTaskListener;
        return this;
    }

    public QuickWorkFlow build(){
        if(null==dao){
            throw new IllegalArgumentException("请配置数据库连接池!");
        }
        QuickFlow workflow = QuickFlowBuilder.newInstance()
                .putContextTemporaryInstanceData(dao, DAO.class)
                .putContextTemporaryInstanceData(quickWorkFlowOption);
        QuickWorkFlowConfig quickWorkFlowConfig = new QuickWorkFlowConfig();
        quickWorkFlowConfig.workflow = workflow;
        WorkFlowInvocationHandler workFlowInvocationHandler = new WorkFlowInvocationHandler(quickWorkFlowConfig);
        QuickWorkFlow quickWorkFlowProxy = (QuickWorkFlow) Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(),
                new Class<?>[]{QuickWorkFlow.class}, workFlowInvocationHandler);
        startWorkFlowTaskTimeoutScheduleService(workflow);
        return quickWorkFlowProxy;
    }

    private void startWorkFlowTaskTimeoutScheduleService(QuickFlow workflow){
        ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);

        scheduler.scheduleAtFixedRate(() -> {
            try {
                workflow.startFlow(new CheckTaskTimeoutServiceFlow()).execute();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }, 0, 1, TimeUnit.SECONDS);
    }

}
